home *** CD-ROM | disk | FTP | other *** search
/ Die Speccy' 97 / Die Speccy' 97.iso / amiga_system / the_aminet / dev / lang / python_src.lha / amigapython / objects / methodobject.c < prev    next >
C/C++ Source or Header  |  1995-10-22  |  6KB  |  261 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Method object implementation */
  26.  
  27. #include "allobjects.h"
  28.  
  29. #include "token.h"
  30.  
  31. typedef struct {
  32.     OB_HEAD
  33.     struct methodlist *m_ml;
  34.     object    *m_self;
  35. } methodobject;
  36.  
  37. object *
  38. newmethodobject(ml, self)
  39.     struct methodlist *ml;
  40.     object *self;
  41. {
  42.     methodobject *op = NEWOBJ(methodobject, &Methodtype);
  43.     if (op != NULL) {
  44.         op->m_ml = ml;
  45.         XINCREF(self);
  46.         op->m_self = self;
  47.     }
  48.     return (object *)op;
  49. }
  50.  
  51. method
  52. getmethod(op)
  53.     object *op;
  54. {
  55.     if (!is_methodobject(op)) {
  56.         err_badcall();
  57.         return NULL;
  58.     }
  59.     return ((methodobject *)op) -> m_ml -> ml_meth;
  60. }
  61.  
  62. object *
  63. getself(op)
  64.     object *op;
  65. {
  66.     if (!is_methodobject(op)) {
  67.         err_badcall();
  68.         return NULL;
  69.     }
  70.     return ((methodobject *)op) -> m_self;
  71. }
  72.  
  73. int
  74. getflags(op)
  75.     object *op;
  76. {
  77.     if (!is_methodobject(op)) {
  78.         err_badcall();
  79.         return -1;
  80.     }
  81.     return ((methodobject *)op) -> m_ml -> ml_flags;
  82. }
  83.  
  84. /* Methods (the standard built-in methods, that is) */
  85.  
  86. static void
  87. meth_dealloc(m)
  88.     methodobject *m;
  89. {
  90.     XDECREF(m->m_self);
  91.     free((char *)m);
  92. }
  93.  
  94. static object *
  95. meth_getattr(m, name)
  96.     methodobject *m;
  97.     char *name;
  98. {
  99.     if (strcmp(name, "__name__") == 0) {
  100.         return newstringobject(m->m_ml->ml_name);
  101.     }
  102.     if (strcmp(name, "__doc__") == 0) {
  103.         char *doc = m->m_ml->ml_doc;
  104.         if (doc != NULL)
  105.             return newstringobject(doc);
  106.         INCREF(None);
  107.         return None;
  108.     }
  109.     if (strcmp(name, "__self__") == 0) {
  110.         object *self;
  111.         if (getrestricted()) {
  112.             err_setstr(RuntimeError,
  113.              "method.__self__ not accessible in restricted mode");
  114.             return NULL;
  115.         }
  116.         self = m->m_self;
  117.         if (self == NULL)
  118.             self = None;
  119.         INCREF(self);
  120.         return self;
  121.     }
  122.     if (strcmp(name, "__members__") == 0) {
  123.         return mkvalue("[sss]", "__doc__", "__name__", "__self__");
  124.     }
  125.     err_setstr(AttributeError, name);
  126.     return NULL;
  127. }
  128.  
  129. static object *
  130. meth_repr(m)
  131.     methodobject *m;
  132. {
  133.     char buf[200];
  134.     if (m->m_self == NULL)
  135.         sprintf(buf, "<built-in function %.80s>", m->m_ml->ml_name);
  136.     else
  137.         sprintf(buf,
  138.             "<built-in method %.80s of %.80s object at %lx>",
  139.             m->m_ml->ml_name, m->m_self->ob_type->tp_name,
  140.             (long)m->m_self);
  141.     return newstringobject(buf);
  142. }
  143.  
  144. static int
  145. meth_compare(a, b)
  146.     methodobject *a, *b;
  147. {
  148.     if (a->m_self != b->m_self)
  149.         return cmpobject(a->m_self, b->m_self);
  150.     if (a->m_ml->ml_meth == b->m_ml->ml_meth)
  151.         return 0;
  152.     if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0)
  153.         return -1;
  154.     else
  155.         return 1;
  156. }
  157.  
  158. static long
  159. meth_hash(a)
  160.     methodobject *a;
  161. {
  162.     long x;
  163.     if (a->m_self == NULL)
  164.         x = 0;
  165.     else {
  166.         x = hashobject(a->m_self);
  167.         if (x == -1)
  168.             return -1;
  169.     }
  170.     return x ^ (long) a->m_ml->ml_meth;
  171. }
  172.  
  173. typeobject Methodtype = {
  174.     OB_HEAD_INIT(&Typetype)
  175.     0,
  176.     "builtin_function_or_method",
  177.     sizeof(methodobject),
  178.     0,
  179.     (destructor)meth_dealloc, /*tp_dealloc*/
  180.     0,        /*tp_print*/
  181.     (getattrfunc)meth_getattr, /*tp_getattr*/
  182.     0,        /*tp_setattr*/
  183.     (cmpfunc)meth_compare, /*tp_compare*/
  184.     (reprfunc)meth_repr, /*tp_repr*/
  185.     0,        /*tp_as_number*/
  186.     0,        /*tp_as_sequence*/
  187.     0,        /*tp_as_mapping*/
  188.     (hashfunc)meth_hash, /*tp_hash*/
  189. };
  190.  
  191. /* List all methods in a chain -- helper for findmethodinchain */
  192.  
  193. static object *
  194. listmethodchain(chain)
  195.     struct methodchain *chain;
  196. {
  197.     struct methodchain *c;
  198.     struct methodlist *ml;
  199.     int i, n;
  200.     object *v;
  201.     
  202.     n = 0;
  203.     for (c = chain; c != NULL; c = c->link) {
  204.         for (ml = c->methods; ml->ml_name != NULL; ml++)
  205.             n++;
  206.     }
  207.     v = newlistobject(n);
  208.     if (v == NULL)
  209.         return NULL;
  210.     i = 0;
  211.     for (c = chain; c != NULL; c = c->link) {
  212.         for (ml = c->methods; ml->ml_name != NULL; ml++) {
  213.             setlistitem(v, i, newstringobject(ml->ml_name));
  214.             i++;
  215.         }
  216.     }
  217.     if (err_occurred()) {
  218.         DECREF(v);
  219.         return NULL;
  220.     }
  221.     sortlist(v);
  222.     return v;
  223. }
  224.  
  225. /* Find a method in a method chain */
  226.  
  227. object *
  228. findmethodinchain(chain, self, name)
  229.     struct methodchain *chain;
  230.     object *self;
  231.     char *name;
  232. {
  233.     if (strcmp(name, "__methods__") == 0)
  234.         return listmethodchain(chain);
  235.     while (chain != NULL) {
  236.         struct methodlist *ml = chain->methods;
  237.         for (; ml->ml_name != NULL; ml++) {
  238.             if (name[0] == ml->ml_name[0] &&
  239.                 strcmp(name+1, ml->ml_name+1) == 0)
  240.                 return newmethodobject(ml, self);
  241.         }
  242.         chain = chain->link;
  243.     }
  244.     err_setstr(AttributeError, name);
  245.     return NULL;
  246. }
  247.  
  248. /* Find a method in a single method list */
  249.  
  250. object *
  251. findmethod(methods, self, name)
  252.     struct methodlist *methods;
  253.     object *self;
  254.     char *name;
  255. {
  256.     struct methodchain chain;
  257.     chain.methods = methods;
  258.     chain.link = NULL;
  259.     return findmethodinchain(&chain, self, name);
  260. }
  261.